home *** CD-ROM | disk | FTP | other *** search
/ Chip 2006 July / CHIP 2006-07.2.iso / program / web_gelistirme / easyphp1-7_setup.exe / {app} / phpmyadmin / tbl_addfield.php < prev    next >
Encoding:
PHP Script  |  2003-09-07  |  9.3 KB  |  243 lines

  1. <?php
  2. /* $Id: tbl_addfield.php,v 1.41 2003/08/13 08:52:10 nijel Exp $ */
  3. // vim: expandtab sw=4 ts=4 sts=4:
  4.  
  5.  
  6. /**
  7.  * Get some core libraries
  8.  */
  9. require('./libraries/grab_globals.lib.php');
  10. $js_to_run = 'functions.js';
  11. require('./header.inc.php');
  12.  
  13. // Check parameters
  14. PMA_checkParameters(array('db', 'table'));
  15.  
  16.  
  17. /**
  18.  * Defines the url to return to in case of error in a sql statement
  19.  */
  20. $err_url = 'tbl_properties.php?' . PMA_generate_common_url($db, $table);
  21.  
  22.  
  23. /**
  24.  * The form used to define the field to add has been submitted
  25.  */
  26. $abort = false;
  27. if (isset($submit)) {
  28.     $query = '';
  29.  
  30.     // Transforms the radio button field_key into 3 arrays
  31.     $field_cnt = count($field_name);
  32.     for ($i = 0; $i < $field_cnt; ++$i) {
  33.         if (isset(${'field_key_' . $i})) {
  34.             if (${'field_key_' . $i} == 'primary_' . $i) {
  35.                 $field_primary[] = $i;
  36.             }
  37.             if (${'field_key_' . $i} == 'index_' . $i) {
  38.                 $field_index[]   = $i;
  39.             }
  40.             if (${'field_key_' . $i} == 'unique_' . $i) {
  41.                 $field_unique[]  = $i;
  42.             }
  43.         } // end if
  44.     } // end for
  45.     // Builds the field creation statement and alters the table
  46.     for ($i = 0; $i < $field_cnt; ++$i) {
  47.         if (empty($field_name[$i])) {
  48.             continue;
  49.         }
  50.         if (PMA_MYSQL_INT_VERSION < 32306) {
  51.             PMA_checkReservedWords($field_name[$i], $err_url);
  52.         }
  53.  
  54.         $query .= PMA_backquote($field_name[$i]) . ' ' . $field_type[$i];
  55.         if ($field_length[$i] != ''
  56.             && !eregi('^(DATE|DATETIME|TIME|TINYBLOB|TINYTEXT|BLOB|TEXT|MEDIUMBLOB|MEDIUMTEXT|LONGBLOB|LONGTEXT)$', $field_type[$i])) {
  57.             $query .= '(' . $field_length[$i] . ')';
  58.         }
  59.         if ($field_attribute[$i] != '') {
  60.             $query .= ' ' . $field_attribute[$i];
  61.         } else if (PMA_MYSQL_INT_VERSION >= 40100 && $field_charset[$i] != '') {
  62.             $query .= ' CHARACTER SET ' . $field_charset[$i];
  63.         }
  64.         if ($field_default[$i] != '') {
  65.             if (strtoupper($field_default[$i]) == 'NULL') {
  66.                 $query .= ' DEFAULT NULL';
  67.             } else {
  68.                 $query .= ' DEFAULT \'' . PMA_sqlAddslashes($field_default[$i]) . '\'';
  69.             }
  70.         }
  71.         if ($field_null[$i] != '') {
  72.             $query .= ' ' . $field_null[$i];
  73.         }
  74.         if ($field_extra[$i] != '') {
  75.             $query .= ' ' . $field_extra[$i];
  76.             // An auto_increment field must be use as a primary key
  77.             if ($field_extra[$i] == 'AUTO_INCREMENT' && isset($field_primary)) {
  78.                 $primary_cnt = count($field_primary);
  79.                 for ($j = 0; $j < $primary_cnt && $field_primary[$j] != $i; $j++) {
  80.                     // void
  81.                 } // end for
  82.                 if ($field_primary[$j] == $i) {
  83.                     $query .= ' PRIMARY KEY';
  84.                     unset($field_primary[$j]);
  85.                 } // end if
  86.             } // end if (auto_increment)
  87.         }
  88.  
  89.         if ($after_field != '--end--') {
  90.             // Only the first field can be added somewhere else than at the end
  91.             if ($i == 0) {
  92.                 if ($after_field == '--first--') {
  93.                     $query .= ' FIRST';
  94.                 } else {
  95.                     $query .= ' AFTER ' . PMA_backquote(urldecode($after_field));
  96.                 }
  97.             } else {
  98.                 $query .= ' AFTER ' . PMA_backquote($field_name[$i-1]);
  99.             }
  100.         }
  101.         $query .= ', ADD ';
  102.     } // end for
  103.     $query = ereg_replace(', ADD $', '', $query);
  104.  
  105.     // To allow replication, we first select the db to use and then run queries
  106.     // on this db.
  107.     $sql_query     = 'USE ' . PMA_backquote($db);
  108.     $result        = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
  109.     $sql_query     = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD ' . $query;
  110.     $error_create = false;
  111.     $result        = PMA_mysql_query($sql_query)  or $error_create = true;
  112.  
  113.     if ($error_create == false) {
  114.  
  115.         $sql_query_cpy = $sql_query . ';';
  116.  
  117.         // Builds the primary keys statements and updates the table
  118.         $primary = '';
  119.         if (isset($field_primary)) {
  120.             $primary_cnt = count($field_primary);
  121.             for ($i = 0; $i < $primary_cnt; $i++) {
  122.                 $j       = $field_primary[$i];
  123.                 if (!empty($field_name[$j])) {
  124.                     $primary .= PMA_backquote($field_name[$j]) . ', ';
  125.                 }
  126.             } // end for
  127.             $primary     = ereg_replace(', $', '', $primary);
  128.             if (!empty($primary)) {
  129.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD PRIMARY KEY (' . $primary . ')';
  130.                 $result         = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
  131.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  132.             }
  133.         } // end if
  134.  
  135.         // Builds the indexes statements and updates the table
  136.         $index = '';
  137.         if (isset($field_index)) {
  138.             $index_cnt = count($field_index);
  139.             for ($i = 0; $i < $index_cnt; $i++) {
  140.                 $j     = $field_index[$i];
  141.                 if (!empty($field_name[$j])) {
  142.                     $index .= PMA_backquote($field_name[$j]) . ', ';
  143.                 }
  144.             } // end for
  145.             $index     = ereg_replace(', $', '', $index);
  146.             if (!empty($index)) {
  147.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD INDEX (' . $index . ')';
  148.                 $result         = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
  149.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  150.             }
  151.         } // end if
  152.  
  153.         // Builds the uniques statements and updates the table
  154.         $unique = '';
  155.         if (isset($field_unique)) {
  156.             $unique_cnt = count($field_unique);
  157.             for ($i = 0; $i < $unique_cnt; $i++) {
  158.                 $j      = $field_unique[$i];
  159.                 if (!empty($field_name[$j])) {
  160.                     $unique .= PMA_backquote($field_name[$j]) . ', ';
  161.                 }
  162.             } // end for
  163.             $unique = ereg_replace(', $', '', $unique);
  164.             if (!empty($unique)) {
  165.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD UNIQUE (' . $unique . ')';
  166.                 $result         = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
  167.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  168.             }
  169.         } // end if
  170.  
  171.  
  172.         // Builds the fulltext statements and updates the table
  173.         $fulltext = '';
  174.         if (PMA_MYSQL_INT_VERSION >= 32323 && isset($field_fulltext)) {
  175.             $fulltext_cnt = count($field_fulltext);
  176.             for ($i = 0; $i < $fulltext_cnt; $i++) {
  177.                 $j        = $field_fulltext[$i];
  178.                 $fulltext .= PMA_backquote($field_name[$j]) . ', ';
  179.             } // end for
  180.             $fulltext = ereg_replace(', $', '', $fulltext);
  181.             if (!empty($fulltext)) {
  182.                 $sql_query      = 'ALTER TABLE ' . PMA_backquote($table) . ' ADD FULLTEXT (' . $fulltext . ')';
  183.                 $result         = PMA_mysql_query($sql_query) or PMA_mysqlDie('', '', '', $err_url);
  184.                 $sql_query_cpy  .= "\n" . $sql_query . ';';
  185.             }
  186.         } // end if
  187.  
  188.         // garvin: If comments were sent, enable relation stuff
  189.         require('./libraries/relation.lib.php');
  190.         require('./libraries/transformations.lib.php');
  191.  
  192.         $cfgRelation = PMA_getRelationsParam();
  193.  
  194.         // garvin: Update comment table, if a comment was set.
  195.         if (isset($field_comments) && is_array($field_comments) && $cfgRelation['commwork']) {
  196.             @reset($field_comments);
  197.             while(list($fieldindex, $fieldcomment) = each($field_comments)) {
  198.                 PMA_setComment($db, $table, $field_name[$fieldindex], $fieldcomment);
  199.             }
  200.         }
  201.  
  202.         // garvin: Update comment table for mime types [MIME]
  203.         if (isset($field_mimetype) && is_array($field_mimetype) && $cfgRelation['commwork'] && $cfgRelation['mimework'] && $cfg['BrowseMIME']) {
  204.             @reset($field_mimetype);
  205.             while(list($fieldindex, $mimetype) = each($field_mimetype)) {
  206.                 PMA_setMIME($db, $table, $field_name[$fieldindex], $mimetype, $field_transformation[$fieldindex], $field_transformation_options[$fieldindex]);
  207.             }
  208.         }
  209.  
  210.         // Go back to the structure sub-page
  211.         $sql_query = $sql_query_cpy;
  212.         unset($sql_query_cpy);
  213.         $message   = $strTable . ' ' . htmlspecialchars($table) . ' ' . $strHasBeenAltered;
  214.         $active_page = 'tbl_properties_structure.php';
  215.         include('./tbl_properties_structure.php');
  216.         exit();
  217.     } else {
  218.         PMA_mysqlDie('', '', '', $err_url, FALSE);
  219.         // garvin: An error happened while inserting/updating a table definition.
  220.         // to prevent total loss of that data, we embed the form once again.
  221.         // The variable $regenerate will be used to restore data in tbl_properties.inc.php
  222.         $num_fields = $orig_num_fields;
  223.         if (isset($orig_after_field)) {
  224.             $after_field = $orig_after_field;
  225.         }
  226.         $regenerate = true;
  227.     }
  228. } // end do alter table
  229.  
  230. /**
  231.  * Displays the form used to define the new field
  232.  */
  233. if ($abort == FALSE) {
  234.     $action = 'tbl_addfield.php';
  235.     include('./tbl_properties.inc.php');
  236.  
  237.     // Diplays the footer
  238.     echo "\n";
  239.     include('./footer.inc.php');
  240. }
  241.  
  242. ?>
  243.